package vista;

import java.util.Vector;

import javax.swing.JButton;
import javax.swing.table.AbstractTableModel;

public class JTableButtonModel extends AbstractTableModel
{
	private static final long serialVersionUID = 1L;
	private Vector<Vector<Object>> rows = new Vector<Vector<Object>>();
	private Vector<String> columns = new Vector<String>();
	private Vector<String> buttons = new Vector<String>();
	
	public JTableButtonModel(Vector<String> columns)
	{
		this.columns = columns;
	}
	
	public JTableButtonModel(Vector<String> columns, Vector<String> buttons)
	{
		this.columns = columns;
		this.buttons = buttons;
		addButtonsColumns();
	}
	
	private void addButtonsColumns()
	{
		for (int i=0; i<buttons.size(); i++)
			columns.add(buttons.elementAt(i));
	}
	
	private void addButtons(Vector<Object> row)
	{
		for (int i=0; i<buttons.size(); i++)
		{
			JButton btn = new JButton(buttons.elementAt(i));
			btn.setActionCommand(row.elementAt(0).toString());
			btn.setFont(new java.awt.Font("Tahoma",0,10));
			row.add(btn);
		}
	}

	public void addRow(Vector<Object> row)
	{
		addButtons(row);
		rows.add(row);
	}
	
	public void setButtons(Vector<String> buttons)
	{
		this.buttons = buttons;
		addButtonsColumns();
	}
	
	public String getColumnName(int column)
	{ 
		return columns.elementAt(column);
	}

	public int getRowCount()
	{
		return rows.size();
	}

	public int getColumnCount()
	{
		return columns.size();
	}

	public Object getValueAt(int row, int column)
	{ 
		return rows.elementAt(row).elementAt(column);
	}

	public boolean isCellEditable(int row, int column)
	{
		if (columns.size() - buttons.size() > column)
			return false;
		return true;
	}

	@SuppressWarnings("unchecked")
	public Class getColumnClass(int column)
	{
		return getValueAt(0, column).getClass();
	}
	
	public void Clear()
	{
		rows = new Vector<Vector<Object>>();
	}
}
